package com.lyl.spring02;

import com.lyl.spring02.dao.UserDao;
import com.lyl.spring02.entity.Person;
import com.lyl.spring02.serivce.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.lyl.spring02.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

/**
 *
 * 默认的情况下, ioc 容器 只能扫描  主程序所在的包及其子包下的组件
 * SpringBootApplication 加了这个注解的就是主程序
 * 1.放大扫描的包 @ComponentScan(basePackages = "com.lyl")
 */
@SpringBootApplication
//@ComponentScan(basePackages = {"com.lyl.spring02"})
public class Spring02Application {

    public static void main(String[] args) {
        ApplicationContext ioc = SpringApplication.run(Spring02Application.class, args);
        System.out.println("ioc容器启动完成-----");

        Person maven = ioc.getBean("person", Person.class);
        System.out.println(person1);


        Person person2 = ioc.getBean("person", Person.class);
        System.out.println(person2);

    }

    public static void main5(String[] args) {
        ApplicationContext ioc = SpringApplication.run(Spring02Application.class, args);
//        String[] names = ioc.getBeanDefinitionNames();
//        for (int i = 0; i < names.length; i++) {
//            System.out.println(names[i]);
//        }
// 找不到此组件
        UserDao userDao = ioc.getBean("userDao", UserDao.class);
        userDao.add();

        UserService userService = ioc.getBean(UserService.class);
        userService.add();

    }
    public static void main3(String[] args) {
        ApplicationContext ioc = SpringApplication.run(Spring02Application.class, args);

        System.out.println("ioc容器启动完成-----");
        User bean = ioc.getBean(User.class);
        System.out.println(bean);

        User bean2 = ioc.getBean(User.class);
        System.out.println(bean2);

    }


    public static void main2(String[] args) {
        ApplicationContext ioc = SpringApplication.run(Spring02Application.class, args);
        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name + "..");
        }
    }

    public static void main1(String[] args) {
        // 启动springboot 容器

        ApplicationContext ioc= SpringApplication.run(Spring02Application.class, args);
        // 字节码获取对象
//     User user=    ioc.getBean(User.class);
//     System.out.println(user);

//        User user1 = (User)ioc.getBean("user2");
//        System.out.println(user1);

        User user=  ioc.getBean("user1",User.class);
        System.out.println(user);
    }

    /**
     * 通过方法 创建对象 加入到ioc容器的时候, 如果没有指定名字, 那么默认就是这个方法的名称
     * 可以在Bean("")指定了名字, 那么就是这个指定的名字
     * @return
     */

}